package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.req.IdParam;
import com.example.common.resp.Result;
import com.example.springboot.dto.RoleResp;
import com.example.springboot.entity.RoleEntity;
import com.example.springboot.entity.RoutingEntity;
import com.example.springboot.entity.SubmenuEntity;
import com.example.springboot.model.RoleSaveParam;
import com.example.springboot.service.RoleService;
import com.example.springboot.service.RoutingService;
import com.example.springboot.service.SubmenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author he
 * @since 2023-11-14
 */
@Tag(name = "权限")
@RestController
@RequestMapping("/roleEntity")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private RoutingService routingService;
    @Autowired
    private SubmenuService submenuService;

    @Operation(summary = "是否存在")
    @PostMapping("/exist")
    public Integer exist(@RequestBody @Validated IdParam param) {
        QueryWrapper<RoleEntity> roleEntityQueryWrapper = new QueryWrapper<>();
        roleEntityQueryWrapper.eq("id", param.getId());
        if (roleService.count(roleEntityQueryWrapper) == 0) {
            return 0;
        }
        return 1;
    }
    @Operation(summary = "权限列表")
    @GetMapping("/list")
    public Result<List<RoleResp>> list() {
        //结果实体处理
        ArrayList<RoleResp> roleRespArrayList = new ArrayList<>();
        List<RoleEntity> roleEntityList = roleService.list();
        for (int i = 0; i < roleEntityList.size(); i++) {
            RoleResp roleResp = new RoleResp();
            RoleEntity roleEntity = roleEntityList.get(i);
            BeanUtils.copyProperties(roleEntity, roleResp);
            roleRespArrayList.add(roleResp);
        }
        //关联
        for (int i = 0; i < roleRespArrayList.size(); i++) {
            RoleResp roleResp = roleRespArrayList.get(i);
            QueryWrapper<RoutingEntity> routingEntityQueryWrapper = new QueryWrapper<>();
            routingEntityQueryWrapper.eq("roleId", roleResp.getId());
            List<RoutingEntity> routingEntityList = routingService.list(routingEntityQueryWrapper);
            //一级路由
            ArrayList<SubmenuEntity> submenuEntityArrayList = new ArrayList<>();
            for (int j = 0; j < routingEntityList.size(); j++) {
                RoutingEntity routingEntity = routingEntityList.get(j);
                submenuEntityArrayList.add(submenuService.getById(routingEntity.getSubmenuId()));
            }
            roleResp.setSubmenuEntityList(submenuEntityArrayList);
        }
        return Result.success(roleRespArrayList);
    }

    @Operation(summary = "根据id查找")
    @PostMapping("/detail")
    public Result<RoleResp> detail(@RequestBody @Validated IdParam param) {
        //ID是否存在
        if (exist(param) == 0) {
            return Result.failure("权限不存在");
        }
        //查找
        RoleResp roleResp = new RoleResp();
        RoleEntity roleEntity = roleService.getById(param.getId());
        BeanUtils.copyProperties(roleEntity, roleResp);
        //关联列表
        QueryWrapper<RoutingEntity> routingEntityQueryWrapper = new QueryWrapper<>();
        routingEntityQueryWrapper.eq("roleId", param.getId());
        List<RoutingEntity> routingEntityList = routingService.list(routingEntityQueryWrapper);
        //一级路由
        ArrayList<SubmenuEntity> submenuEntityArrayList = new ArrayList<>();
        for (int i = 0; i < routingEntityList.size(); i++) {
            RoutingEntity routingEntity = routingEntityList.get(i);
            QueryWrapper<SubmenuEntity> submenuEntityQueryWrapper = new QueryWrapper<>();
            submenuEntityQueryWrapper.eq("id", routingEntity.getSubmenuId());
            submenuEntityArrayList.add(submenuService.getOne(submenuEntityQueryWrapper));
        }
        roleResp.setSubmenuEntityList(submenuEntityArrayList);
        //返回
        return Result.success(roleResp);
    }

    @Operation(summary = "保存或修改")
    @PostMapping("/save")
    public Result save(@RequestBody @Validated RoleSaveParam param) {
        RoleEntity roleEntity = new RoleEntity();
        BeanUtils.copyProperties(param, roleEntity);
        if (roleEntity.getId() == 0) {
            roleEntity.setId(null);
            roleEntity.setCreateTime(new Date());
            roleEntity.setUpdateTime(new Date());
            if (roleService.save(roleEntity)) {
                return Result.success();
            }
            return Result.failure("保存失败，请稍后重试");
        } else {
            roleEntity.setUpdateTime(new Date());
            if (roleService.updateById(roleEntity)) {
                return Result.success();
            }
            return Result.failure("修改失败，请稍后重试");
        }
    }

    @Operation(summary = "根据id删除")
    @PostMapping("/delete")
    public Result delete(@RequestBody @Validated IdParam param) {
        //ID是否存在
        if (exist(param) == 0) {
            return Result.failure("权限不存在");
        }
        //删除
        if (roleService.removeById(param.getId())) {
            return Result.success();
        } else {
            return Result.failure("删除失败，请稍后重试");
        }
    }

}
